package com.tang.javaSwingCaculate.demo;

import lombok.Data;

/**
 * @author tmh
 * @date 2022/12/4 22:03
 * @description 中辍表达式转换为后辍表达式
 */
@Data
public class InToPost {
    public static final MyStackInteger result = new MyStackInteger(100);
    /**
     * opStack:存放操作过程中的运算符
     */
    private MyStack opStack;

    /**
     * outStack:存放最后的后辍表达式
     */
    private MyStack outStack;

    /**
     * input:表示为中辍表达式，需要被转换
     */
    private String input;

    public InToPost(String input) {
        this.input = input;
        opStack = new MyStack(100);
        outStack = new MyStack(100);
    }

    public MyStack formatTrans() {
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            switch (ch) {
                case '+':
                case '-':
                    operationOpStack(ch, 1);
                    break;
                case '*':
                case '/':
                    operationOpStack(ch, 2);
                    break;
                case '(':
                    opStack.push(ch);
                    break;
                case ')':
                    operationPattern();
                    break;
                default:
                    outStack.push(ch);
                    break;
            }
        }
        while (!opStack.isMyStackEmpty()) {
            outStack.push(opStack.pop());
        }
        return outStack;
    }

    /**
     * 对操作符号的运算
     *
     * @param opThis   当前的操作符
     * @param precise1 优先级 1代表+，-的优先级  2代表*，/的优先级
     */
    public void operationOpStack(char opThis, int precise1) {
//        opStack= convertToChar(opStack);
        while (!opStack.isMyStackEmpty()) {

            char opTop = opStack.pop();
            if (opTop == '(') {
                opStack.push(opTop);
                break;
            } else {
                int precise2;
                if (opTop == '+' || opTop == '-') {
                    precise2 = 1;
                } else {
                    precise2 = 2;
                }
                if (precise1 > precise2) {
                    opStack.push(opTop);
                    break;
                } else {
                    outStack.push(opTop);
                }
            }
        }
        opStack.push(opThis);
    }

    /**
     * 对（ 的具体操作
     * 如果遇到了）,那么就把（之前的运算符出栈放到outStack里面
     */
    public void operationPattern() {
        while (!opStack.isMyStackEmpty()) {
            char c = opStack.pop();
            if (c == '(') {
                break;
            } else {
                outStack.push(c);
            }

        }
    }

    /**
     * 计算出最后后缀表达式的四则运算表达式的结果
     *
     * @return result
     */
    public Integer calculateResult() {
//        MyStackInteger result = new MyStackInteger(100);
        int myStackLength = outStack.length();
        if (myStackLength == 0) {
            return null;
        }
        char[] temp = new char[100];
        //将character类型转换为char类型--拆箱
        for (int i = 0; i < myStackLength; i++) {
            temp[i] = outStack.getStackArray()[i].charValue();
//            temp[i] = outStack.getStackArray()[i];
        }
        for (int i = 0; i < myStackLength; i++) {
            if ("+".equals(String.valueOf(temp[i]))) {
                result.push(result.pop() + result.pop());
            } else if ("-".equals(String.valueOf(temp[i]))) {
                result.push(-result.pop() + result.pop());
            } else if ("*".equals(String.valueOf(temp[i]))) {
                result.push(result.pop() * result.pop());
            } else if ("/".equals(String.valueOf(temp[i]))) {
                Integer temp1 = result.pop();
                Integer temp2 = result.pop();
                result.push(temp2 / temp1);
            } else {
                result.push(Integer.valueOf(String.valueOf(temp[i])));
            }
        }
        return result.pop();
    }

}
